#include <QApplication>
#include <QMessageBox>

#include "csyncprocessdialog.h"
#include "ui_csyncprocessdialog.h"

CSyncProcessDialog::CSyncProcessDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CSyncProcessDialog)
{
    ui->setupUi(this);
}

CSyncProcessDialog::~CSyncProcessDialog()
{
    delete ui;
}

void CSyncProcessDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CSyncProcessDialog::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    if(ui->recvBar->maximum()!=bytesTotal)
        ui->recvBar->setMaximum(bytesTotal);

    QApplication::processEvents();

    ui->recvBar->setValue(bytesReceived);

    if(bytesReceived==bytesTotal)
    {
        ui->buttonBox->setStandardButtons(QDialogButtonBox::Close);
    }
}

void CSyncProcessDialog::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
    if(ui->sendBar->maximum()!=bytesTotal)
        ui->sendBar->setMaximum(bytesTotal);

    ui->sendBar->setValue(bytesSent);
}

void CSyncProcessDialog::error(QNetworkReply::NetworkError code)
{
    if(code==QNetworkReply::NoError || code==QNetworkReply::UnknownContentError ||
       code==QNetworkReply::UnknownNetworkError || code==QNetworkReply::ProtocolFailure)
        return;

    QMessageBox::critical(this, tr("Sync error"), tr("Error code: %1").arg(code));
}

